package se.gu.sem;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import twitter4j.Status;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class GUActivity extends Activity {
	
	//number of tweets to show
	private int notweets = 3;
	 private Button program_info;
   	private Button practical_info;
   	private Button twitter;
   	private Button register;
   	private Button about;
   	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        GUApp univ = ((GUApp)getApplicationContext());
        
        if(univ.getUniv().equals("GU"))
        	setContentView(R.layout.gumain);
        else if(univ.getUniv().equals("CH"))
            setContentView(R.layout.chmain);
        
        setUpViews();
        
        
        andwitter("SE_Master1");
    }
    

    private void setUpViews(){
    	
	program_info = (Button)findViewById(R.id.programInfo_button);
	practical_info = (Button)findViewById(R.id.practicalInfo_button);
	twitter=(Button)findViewById(R.id.twitter_button);
	register=(Button)findViewById(R.id.register_button);
	about=(Button)findViewById(R.id.about_button);
	program_info.setOnClickListener(new View.OnClickListener(){

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(GUActivity.this,ProgrameInfoActivity.class);
			startActivity(intent);	
		}   		
	});
 
 	practical_info.setOnClickListener(new View.OnClickListener(){
		public void onClick(View v) {
			Intent intent = new Intent(GUActivity.this,PracticalInfo.class);
			startActivity(intent);	
		}   		
	});
	register.setOnClickListener(new View.OnClickListener(){

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(GUActivity.this,RegisterActivity.class);
			startActivity(intent);	
		}   		
	});
	
	about.setOnClickListener(new View.OnClickListener(){

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(GUActivity.this,AboutActivity.class);
			startActivity(intent);	
		}   		
	});
	
	twitter.setOnClickListener(new View.OnClickListener(){

		@Override
		public void onClick(View v) {			
			
			GUApp univ = ((GUApp)getApplicationContext());
			univ.setView("twitter");
  	  
			Intent intent = new Intent(GUActivity.this,LoadingScreen.class);
			startActivity(intent);	
		}   		
	});

    } 
    
    
    
    private void andwitter(String twitter_id)
	{
		TwitterFetch tf = new TwitterFetch();
		List<Status> status = tf.twitter_fetch(twitter_id);
		ArrayList<String[]> str_list = new ArrayList<String[]>();
		LayoutInflater inflater = getLayoutInflater();
		TableLayout tl = (TableLayout)findViewById(R.id.twitterTL);
		if(status.isEmpty())
		{
			TableRow tr = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
			TextView tw2 = (TextView)inflater.inflate(R.layout.twtweettext, tr, false);
			tw2.setText("There are no tweets to show!");
		}
		else
		{
			for (Status st : status)
			{
				int i = 1;
				String[] str_arr = new String[3];
				String sts = st.getText();
				if(sts.startsWith("RT @"))
				{
					String[] str1 = sts.split(":", 2);
					String[] str2 = str1[0].split("RT ");
					str_arr[0] = str2[1];
					str_arr[1] = str1[1];
				}
				else
				{
					str_arr[0] = "SE_Master"; //st.getUser().getName();
					str_arr[1] = st.getText();
				}
				Date dt = st.getCreatedAt();
				Date nw = new Date();
				String ufdate = user_friendly_date(nw.getTime(), dt.getTime());
				str_arr[2] = ufdate;
				str_list.add(str_arr);
				i++;
				if(i >= notweets)
					return;
			}
			int tweet_num;
			if(notweets <= status.size())
				tweet_num = notweets;
			else
				tweet_num = status.size();
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			for(int i=0; i< tweet_num; i++)
			{
				TableRow tr1 = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
				TableRow tr2 = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
				TableRow tr3 = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
				TextView tw1 = (TextView)inflater.inflate(R.layout.twtweetauthor, tr1, false);
				TextView tw2 = (TextView)inflater.inflate(R.layout.twtweettext, tr2, false);
				TextView tw3 = (TextView)inflater.inflate(R.layout.twtweetdate, tr3, false);
				tw2.setMovementMethod(LinkMovementMethod.getInstance());
				tw1.setText(str_list.get(i)[0]);
				tw2.setText(str_list.get(i)[1]);
				tw3.setText(str_list.get(i)[2]);
				tr1.addView(tw1);
				tr2.addView(tw2);
				tr3.addView(tw3);
				lp.setMargins(10, 0, 10, 0);
				tl.addView(tr1, lp);
				tl.addView(tr2, lp);
				tl.addView(tr3, lp);
			}
		}
	}
	
	private String user_friendly_date(long tm1, long tm2)
	{
		//difference in hours
		long diff = (tm1 - tm2)/(1000*60*60);
		if(diff < 24)
			return String.valueOf(diff) + " hours ago";
		else if(diff >=24 && diff < 48)
			return "yesterday";
		else
			return String.valueOf(diff/24) + " days ago";
	}
	

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menumain, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {    	
    if(item.getTitle().toString().equals("Practical info"))
    {	

    	   Intent intent = new Intent(GUActivity.this,PracticalInfo.class);
    	   startActivity(intent);    
    	   return true;
     }
    if(item.getTitle().toString().equals("Register for an exam"))
    {    

   	   Intent intent = new Intent(GUActivity.this,RegisterActivity.class);
   	   startActivity(intent);    
   	   return true;
    }
      if(item.getTitle().toString().equals("Program Info"))
      {

      	Intent intent = new Intent(GUActivity.this,ProgrameInfoActivity.class);
      	startActivity(intent);    
      	return true;   
      }
      
      if(item.getTitle().toString().equals("Twitter"))
      {    
			GUApp univ = ((GUApp)getApplicationContext());
			univ.setView("twitter");
    	  
     	   Intent intent = new Intent(GUActivity.this,LoadingScreen.class);
     	   startActivity(intent);    
     	   return true;
      }
       
      if(item.getTitle().toString().equals("About"))
      {    

     	   Intent intent = new Intent(GUActivity.this,AboutActivity.class);
     	   startActivity(intent);    
     	   return true;
      }
       
     return super.onOptionsItemSelected(item);
    
    }
    
    
}